Aufgabe_1

windijon

2022-04-22

Task 1: Import data

library(readr)        # to import tabular data (e.g. csv)
library(dplyr)        # to manipulate (tabular) data
library(ggplot2)      # to visualize data

Wildschwein_Daten <- read_csv(file = "wildschwein_BE.csv")
View(Wildschwein_Daten)

Task 2: Explore Data

ggplot(Wildschwein_Daten, aes(Long, Lat, colour = TierID)) +
  geom_point()

Input I: Handling spatial data (Tabelle in Spatial Object umwandeln)

library(sf)

wildschwein_BE_sf <- st_as_sf(Wildschwein_Daten,             # Tabelle in Spatial Object umwandeln
                              coords = c("Long", "Lat"), 
                              crs = 4326)

View(Wildschwein_Daten)

wildschwein_BE_sf
## Simple feature collection with 51246 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 7.019889 ymin: 46.97125 xmax: 7.112075 ymax: 47.01882
## Geodetic CRS:  WGS 84
## # A tibble: 51,246 × 5
##    TierID TierName CollarID DatetimeUTC                    geometry
##  * <chr>  <chr>       <dbl> <dttm>                      <POINT [°]>
##  1 002A   Sabi        12275 2014-08-22 21:00:12 (7.049618 46.99317)
##  2 002A   Sabi        12275 2014-08-22 21:15:16 (7.049509 46.99416)
##  3 002A   Sabi        12275 2014-08-22 21:30:43 (7.049406 46.99383)
##  4 002A   Sabi        12275 2014-08-22 21:46:07 (7.049217 46.99375)
##  5 002A   Sabi        12275 2014-08-22 22:00:22 (7.049359 46.99375)
##  6 002A   Sabi        12275 2014-08-22 22:15:10 (7.049363 46.99382)
##  7 002A   Sabi        12275 2014-08-22 22:30:13 (7.049326 46.99387)
##  8 002A   Sabi        12275 2014-08-22 22:45:11 (7.049237 46.99395)
##  9 002A   Sabi        12275 2014-08-22 23:00:27 (7.048383 46.99481)
## 10 002A   Sabi        12275 2014-08-22 23:15:41 (7.049396 46.99373)
## # … with 51,236 more rows
is.data.frame(wildschwein_BE_sf) # -> [1] TRUE
## [1] TRUE
# subset rows
wildschwein_BE_sf[1:10,]
## Simple feature collection with 10 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 7.048383 ymin: 46.99317 xmax: 7.049618 ymax: 46.99481
## Geodetic CRS:  WGS 84
## # A tibble: 10 × 5
##    TierID TierName CollarID DatetimeUTC                    geometry
##    <chr>  <chr>       <dbl> <dttm>                      <POINT [°]>
##  1 002A   Sabi        12275 2014-08-22 21:00:12 (7.049618 46.99317)
##  2 002A   Sabi        12275 2014-08-22 21:15:16 (7.049509 46.99416)
##  3 002A   Sabi        12275 2014-08-22 21:30:43 (7.049406 46.99383)
##  4 002A   Sabi        12275 2014-08-22 21:46:07 (7.049217 46.99375)
##  5 002A   Sabi        12275 2014-08-22 22:00:22 (7.049359 46.99375)
##  6 002A   Sabi        12275 2014-08-22 22:15:10 (7.049363 46.99382)
##  7 002A   Sabi        12275 2014-08-22 22:30:13 (7.049326 46.99387)
##  8 002A   Sabi        12275 2014-08-22 22:45:11 (7.049237 46.99395)
##  9 002A   Sabi        12275 2014-08-22 23:00:27 (7.048383 46.99481)
## 10 002A   Sabi        12275 2014-08-22 23:15:41 (7.049396 46.99373)
wildschwein_BE_sf[wildschwein_BE_sf$TierName == "Sabi",]
## Simple feature collection with 22746 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 7.029718 ymin: 46.98947 xmax: 7.059671 ymax: 47.01454
## Geodetic CRS:  WGS 84
## # A tibble: 22,746 × 5
##    TierID TierName CollarID DatetimeUTC                    geometry
##    <chr>  <chr>       <dbl> <dttm>                      <POINT [°]>
##  1 002A   Sabi        12275 2014-08-22 21:00:12 (7.049618 46.99317)
##  2 002A   Sabi        12275 2014-08-22 21:15:16 (7.049509 46.99416)
##  3 002A   Sabi        12275 2014-08-22 21:30:43 (7.049406 46.99383)
##  4 002A   Sabi        12275 2014-08-22 21:46:07 (7.049217 46.99375)
##  5 002A   Sabi        12275 2014-08-22 22:00:22 (7.049359 46.99375)
##  6 002A   Sabi        12275 2014-08-22 22:15:10 (7.049363 46.99382)
##  7 002A   Sabi        12275 2014-08-22 22:30:13 (7.049326 46.99387)
##  8 002A   Sabi        12275 2014-08-22 22:45:11 (7.049237 46.99395)
##  9 002A   Sabi        12275 2014-08-22 23:00:27 (7.048383 46.99481)
## 10 002A   Sabi        12275 2014-08-22 23:15:41 (7.049396 46.99373)
## # … with 22,736 more rows
# subset colums
wildschwein_BE_sf[,2:3]
## Simple feature collection with 51246 features and 2 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 7.019889 ymin: 46.97125 xmax: 7.112075 ymax: 47.01882
## Geodetic CRS:  WGS 84
## # A tibble: 51,246 × 3
##    TierName CollarID            geometry
##    <chr>       <dbl>         <POINT [°]>
##  1 Sabi        12275 (7.049618 46.99317)
##  2 Sabi        12275 (7.049509 46.99416)
##  3 Sabi        12275 (7.049406 46.99383)
##  4 Sabi        12275 (7.049217 46.99375)
##  5 Sabi        12275 (7.049359 46.99375)
##  6 Sabi        12275 (7.049363 46.99382)
##  7 Sabi        12275 (7.049326 46.99387)
##  8 Sabi        12275 (7.049237 46.99395)
##  9 Sabi        12275 (7.048383 46.99481)
## 10 Sabi        12275 (7.049396 46.99373)
## # … with 51,236 more rows
wildschwein_BE <- st_as_sf(Wildschwein_Daten, 
                          coords = c("Long", "Lat"), 
                          crs = 4326)

rm(wildschwein_BE_sf) 
# we can remove this sf object, since it just eats up our memory

Task 3: Project data from WGS84

st_transform(x = wildschwein_BE, crs =  2056)
## Simple feature collection with 51246 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 2568153 ymin: 1202306 xmax: 2575154 ymax: 1207609
## Projected CRS: CH1903+ / LV95
## # A tibble: 51,246 × 5
##    TierID TierName CollarID DatetimeUTC                  geometry
##  * <chr>  <chr>       <dbl> <dttm>                    <POINT [m]>
##  1 002A   Sabi        12275 2014-08-22 21:00:12 (2570409 1204752)
##  2 002A   Sabi        12275 2014-08-22 21:15:16 (2570402 1204863)
##  3 002A   Sabi        12275 2014-08-22 21:30:43 (2570394 1204826)
##  4 002A   Sabi        12275 2014-08-22 21:46:07 (2570379 1204817)
##  5 002A   Sabi        12275 2014-08-22 22:00:22 (2570390 1204818)
##  6 002A   Sabi        12275 2014-08-22 22:15:10 (2570390 1204825)
##  7 002A   Sabi        12275 2014-08-22 22:30:13 (2570387 1204831)
##  8 002A   Sabi        12275 2014-08-22 22:45:11 (2570381 1204840)
##  9 002A   Sabi        12275 2014-08-22 23:00:27 (2570316 1204935)
## 10 002A   Sabi        12275 2014-08-22 23:15:41 (2570393 1204815)
## # … with 51,236 more rows

Input II: Calculate Convex Hull (aus Punktwolke Polygone machen)

wildschwein_BE_grouped <- group_by(wildschwein_BE,TierID)
wildschwein_BE_grouped
## Simple feature collection with 51246 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 7.019889 ymin: 46.97125 xmax: 7.112075 ymax: 47.01882
## Geodetic CRS:  WGS 84
## # A tibble: 51,246 × 5
## # Groups:   TierID [3]
##    TierID TierName CollarID DatetimeUTC                    geometry
##    <chr>  <chr>       <dbl> <dttm>                      <POINT [°]>
##  1 002A   Sabi        12275 2014-08-22 21:00:12 (7.049618 46.99317)
##  2 002A   Sabi        12275 2014-08-22 21:15:16 (7.049509 46.99416)
##  3 002A   Sabi        12275 2014-08-22 21:30:43 (7.049406 46.99383)
##  4 002A   Sabi        12275 2014-08-22 21:46:07 (7.049217 46.99375)
##  5 002A   Sabi        12275 2014-08-22 22:00:22 (7.049359 46.99375)
##  6 002A   Sabi        12275 2014-08-22 22:15:10 (7.049363 46.99382)
##  7 002A   Sabi        12275 2014-08-22 22:30:13 (7.049326 46.99387)
##  8 002A   Sabi        12275 2014-08-22 22:45:11 (7.049237 46.99395)
##  9 002A   Sabi        12275 2014-08-22 23:00:27 (7.048383 46.99481)
## 10 002A   Sabi        12275 2014-08-22 23:15:41 (7.049396 46.99373)
## # … with 51,236 more rows
wildschwein_BE_sample <- sample_n(wildschwein_BE_grouped, 2000)

wildschwein_BE_smry <- summarise(wildschwein_BE_sample)
wildschwein_BE_smry
## Simple feature collection with 3 features and 1 field
## Geometry type: MULTIPOINT
## Dimension:     XY
## Bounding box:  xmin: 7.02001 ymin: 46.97184 xmax: 7.111014 ymax: 47.0188
## Geodetic CRS:  WGS 84
## # A tibble: 3 × 2
##   TierID                                                                geometry
##   <chr>                                                         <MULTIPOINT [°]>
## 1 002A   ((7.043728 46.99472), (7.043766 46.99469), (7.043629 46.99456), (7.043…
## 2 016A   ((7.043067 46.99562), (7.043099 46.99563), (7.043112 46.99566), (7.043…
## 3 018A   ((7.043432 46.99525), (7.043432 46.9953), (7.043325 46.99532), (7.0428…
mcp <- st_convex_hull(wildschwein_BE_smry)
mcp
## Simple feature collection with 3 features and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 7.02001 ymin: 46.97184 xmax: 7.111014 ymax: 47.0188
## Geodetic CRS:  WGS 84
## # A tibble: 3 × 2
##   TierID                                                                geometry
## * <chr>                                                            <POLYGON [°]>
## 1 002A   ((7.048278 46.99006, 7.044914 46.99263, 7.044254 46.99314, 7.033533 47…
## 2 016A   ((7.074761 46.97184, 7.041213 46.98309, 7.034756 47.00236, 7.034853 47…
## 3 018A   ((7.043432 46.99525, 7.02001 47.00084, 7.021248 47.01572, 7.03724 47.0…

Task 4: Ploting spatial objects

plot(mcp)

ggplot(mcp, aes(colour = TierID)) +
  geom_sf() +
  coord_sf(datum =2056)

Input III: Importing raster data (import swisstopo-Map)

library(terra)
  
pk100_BE <- terra::rast("pk100_BE.tif")
pk100_BE
## class       : SpatRaster 
## dimensions  : 1821, 2321, 3  (nrow, ncol, nlyr)
## resolution  : 5, 5  (x, y)
## extent      : 2567000, 2578605, 1199996, 1209101  (xmin, xmax, ymin, ymax)
## coord. ref. : CH1903+ / LV95 (EPSG:2056) 
## source      : pk100_BE.tif 
## names       : pk1_1, pk1_2, pk1_3 
## min values  :     0,     0,     0 
## max values  :   255,   255,   255
plot(pk100_BE)

plotRGB(pk100_BE)

Task 5: Adding a background map (Karte und Wildschwein-Daten zusammen plotten)

library(tmap)

tmap_mode("plot")

map1 <- tm_shape(shp = pk100_BE) + 
  tm_rgb() +
  tm_shape(shp = mcp) +
  tm_polygons("TierID", border.col = "black", lwd = 1.5, alpha = 0.4) +   # alpha = transparency
  tm_layout(legend.title.size = 1,
            legend.text.size = 0.6,
            legend.position = c("left","top"),
            legend.bg.color = "white",
            legend.bg.alpha = 1,
            frame = F)
  
map1

Task 6: Create an interactive map

map1 <- tm_shape(shp = pk100_BE) + 
  tm_rgb() +
  tm_shape(shp = mcp) +
  tm_polygons("TierID", border.col = "black", lwd = 1.5, alpha = 0.4) +   # alpha = transparency
  tm_compass(type = "arrow", position = c(0.23,0.021), bg.color = "white", bg.alpha = 0.75, size = 1.8) + 
  tm_scale_bar(breaks = c(0, 0.5, 1, 1.5, 2), text.size = 0.75, position = c("left", "bottom"), bg.color = "white", bg.alpha = 0.75) +
  tm_credits("Scale: 1:25'000 \nData source: swisstopo, ZHAW \nAuthor: Jonas M. Windisch", fontface = "italic",  bg.color = "white", bg.alpha = 0.75, width = 1, position = c(0.73,0.02)) +
  tm_logo(file = "zhaw_lsfm_iunr_schwarz.png", position = c(0.79,0.86)) + 
  tm_layout(main.title = " Movement Patterns of Wild-Boar (Sus Scrofa) in the Seeland District (2014-2015)",
            main.title.size = 1,
            legend.title.size = 1,
            legend.text.size = 0.6,
            legend.position = c("left","top"),
            legend.bg.color = "white",
            legend.bg.alpha = 1,
            frame = F)

map1

tmap_mode("view")
map1  + tm_minimap()
tmap_mode("plot")
tmap_save(tm = map1, filename = "Aufgabe1.pdf", width = 17, height = 14, units = "cm", dpi = 5000)